package com.hopool.shop.dao;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import org.hibernate.Criteria;

public class PageUtil {
	public static final String DEFAULT_ORDER_PROPERTY = "createDate";
	public static final String ORDER_ASC = "asc";
	public static final String ORDER_DESC = "desc";
	
	public synchronized static boolean isExistCreateTimeProp(Object obj){
		Class clazz = obj.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for(Field f:fields){
			String fieldName = f.getName();
			System.out.println(fieldName);
			if(DEFAULT_ORDER_PROPERTY.toLowerCase().equals(fieldName.toLowerCase())){
				return true;
			}
		}
		return false;
	}
	
	public synchronized static Object getCustomProperty(Object objExample,String fieldName){
		String method = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
		Object fieldValue =null;
		try {
			fieldValue = objExample.getClass().getMethod(method).invoke(objExample);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return fieldValue;
	}
	
	public static boolean isPrimitive(Class clazz){
		try {
			return ((Class) clazz.getField("TYPE").get(null)).isPrimitive();
		}catch (Exception e) {
			return false;
		}
	}
}
